<?php
namespace App\Http\Controllers\ApiAdmin;

use App\Service\Admin\YiyuanService;
use Illuminate\Http\Request;

class YiyuanController extends BaseController
{
    //医生

    public function __construct(YiyuanService $service)
    {
        $this->db = $service;
    }

    //列表
    public function getIndex(Request $request)
    {
        $pages['start'] = $request->post('page', 1);
        $pages['limit'] = $request->post('limit', 10);
        $res = $this->db->getList($pages);
        if ($res['code'] < 0) {
            return $this->toJson($res['code'], $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //一个
    public function getOne(Request $request)
    {
        $id = $request->post('id', 0);
        $res = $this->db->getOne($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //修改
    public function modify(Request $request)
    {
        $id = $request->post('id', 0);
        $pas['title'] = $request->post('title', '');
        $pas['logo'] = $request->post('logo', '');
        $pas['address'] = $request->post('address', '');
        $pas['map'] = $request->post('map', '');
        if (!$pas['title']) {
            return $this->toJson(-2, '门店名称必须的');
        }
        if (!$id) {
            $res = $this->db->addOne($pas);
        } else {
            $res = $this->db->modify($id, $pas);
        }
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //删除
    public function setDel(Request $request)
    {
        $id = $request->post('id', 0);
        if (!$id) {
            return $this->toJson(-2, '门店ID必须的');
        }
        $res = $this->db->setDel($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //地址转坐标
    public function addressToMap(Request $request)
    {
        $address = $request->post('address', '');
        $httpUrl = 'https://apis.map.qq.com/ws/geocoder/v1/';
        $httpUrl .= '?address=' . $address;
        $httpUrl .= '&key=JWYBZ-7LK3K-KRNJA-AGOHX-CIZNK-RCFXV';
        $res = $this->setCurl($httpUrl);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        $res = $res['data'];
        if ($res['status'] != 0) {
            return $this->toJson(-2, $res['message']);
        }
        return $this->toJson(0, '', $res['result']);
    }
}
